package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.Result;
import com.mrobuy.jdvc.dao.VCCateBrandDao;
import com.mrobuy.jdvc.entity.Task;
import com.mrobuy.jdvc.entity.VCCateBrand;
import com.mrobuy.util.MD5Utils;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("vcCateBrandDao")
public class VCCateBrandDaoImpl implements VCCateBrandDao {

    private static Logger LOG = LoggerFactory.getLogger(VCCateBrandDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCCateBrand> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_CATE_BRAND");
        try {
            List<VCCateBrand> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCCateBrand.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public List<VCCateBrand> list(String vendor) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_CATE_BRAND where ACCOUNT = ?");
        try {
            List<VCCateBrand> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCCateBrand.class), vendor);
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, VCCateBrand condition) {


        Pagination pagination = new Pagination();


        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");

        if ( condition.getCateid()!=null  ) {
            whereCond.append("  and CATEID = ? ");
            params.add(condition.getCateid());
        }

        if ( condition.getBrandid()!=null  ) {
            whereCond.append("  and BRANDID = ? ");
            params.add(condition.getBrandid());
        }

        try {
            int count = jdbcTemplate.queryForObject("select count(*) from VC_CATE_BRAND " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);
            List<VCCateBrand> list = jdbcTemplate.query("select * from VC_CATE_BRAND " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCCateBrand.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }


    @Override
    public List<VCCateBrand> query(String cid1, String brandid) {

        List<Object> params = new ArrayList<>();

        StringBuilder sqlBuild = new StringBuilder("select * from VC_CATE_BRAND where 1 = 1 ");
        if ( StringUtils.isNotBlank(cid1) ) {
            sqlBuild.append(" and CATEID = ? ");
            params.add(cid1);
        }

        if ( StringUtils.isNotBlank(brandid) ) {
            sqlBuild.append(" and BRANDID = ? ");
            params.add(brandid);
        }

        try {
            List<VCCateBrand> list = jdbcTemplate.query(sqlBuild.toString(), params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCCateBrand.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Result insert(VCCateBrand record) throws Exception {

        Result result = new Result(true);

        if ( record==null ) {
            return result;
        }

        if ( StringUtils.isBlank(record.getId()) ) {
            record.setId(MD5Utils.md5Hex(record.getCateid() + "_" + record.getBrandid()));
        }

        List<VCCateBrand> exists = jdbcTemplate.query("select * from VC_CATE_BRAND where ID = ? ", new Object[] { record.getId() }, new BeanPropertyRowMapper(VCCateBrand.class));
        if ( exists!=null && exists.size() > 0 ) {
            result.setSuccess(false);
            result.setMsg("类目[" + record.getCatename() + "]下品牌[" + record.getName() + "]已经存在");
            return result;
        }

        SQLExecutor.tempLateInsert(record, jdbcTemplate);

        return result;
    }

    @Override
    public List<VCCateBrand> available(String vendorCode) {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_CATE_BRAND where STATUS = 'Y' and ACCOUNT = ?  ");
        try {
            List<VCCateBrand> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCCateBrand.class), vendorCode);
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }
}
